import { list, getOrderId } from "../../../../api/order";
const util = require("../../../../utils/util.js");
let app = getApp();
Page({
  data: {
    currentTab: 0,
    pageNum: 1,
    pageSize: 5,
    orderList: [],
    paymentStatus: 0, // 付款状态
    shippingStatus: 0, // 配送状态
    orderStatus: 0, // 订单状态
    loading: true,
    arr: [],
    triggered: false,
    refreshType: false,
  },
  onLoad: function (query) {
    var that = this;
    /**
     * 获取当前设备的宽高
     */
    this.setNavSize();
    this.setData({
      paymentStatus: "", // 付款状态
      shippingStatus: "", // 配送状态
      orderStatus: "", // 订单状态
      currentTab: query.index,
    });
    this.data.refreshType = true;
    if (query.channel == "1") {
      wx.showLoading({
        title: "加载订单中",
      });
      let timer = setInterval(() => {
        if (app.globalData.token != null) {
          getOrderId({ out_trade_no: query.id }).then((res) => {
            if (res.type == "success") {
              wx.hideLoading();
              wx.navigateTo({
                url: "/pages/order/jumpPage/order/details?id=" + res.data,
              });
            }
          });
          clearInterval(timer);
        }
      }, 200);
    }
    if (query.index == 0) {
      this.bindChange();
    }
  },
  onShow: function () {
    if (this.data.refreshType == false) {
      this.bindChange();
    }
    this.data.refreshType = false;
    wx.onAppShow((options) => {
      if (options.scene === 1037 || options.scene === 1038) {
        wx.showLoading({
          title: "查询支付状态",
        });
        if (!util.isNull(options.referrerInfo.extraData)) {
          const { extraData } = options.referrerInfo;
          if (extraData.status == "success") {
            wx.hideLoading();
            this.loadList();
          } else {
            wx.hideLoading();
            wx.showToast({
              title: "支付失败",
              icon: "none",
            });
          }
        } else {
          query({
            sn: this.data.sn,
          })
            .then((res) => {
              wx.hideLoading();
              this.loadList();
            })
            .catch((error) => {
              wx.hideLoading();
              wx.showToast({
                title: "支付失败",
                icon: "none",
              });
            });
        }
      }
    });
  },
  //  tab切换逻辑
  swichNav: function (e) {
    var _this = this;
    if (e !== undefined) {
      if (this.data.currentTab === e.target.dataset.current) {
        return false;
      } else {
        _this.data.currentTab = e.target.dataset.current;
        _this.setData({
          currentTab: e.target.dataset.current,
        });
      }
    }
  },

  bindChange: function (e) {
    var _this = this;
    if (!util.isNull(e)) {
      _this.data.currentTab = e.detail.current;
    }
    this.data.pageNum = 1;
    this.data.orderList = [];
    var paymentStatus = ""; // 付款状态
    var shippingStatus = ""; // 配送状态
    var orderStatus = "";
    switch (_this.data.currentTab) {
      case 0:
        paymentStatus = ""; // 付款状态
        shippingStatus = ""; // 配送状态
        orderStatus = ""; // 订单状态
        break;
      case 1:
        paymentStatus = 0; // 付款状态
        shippingStatus = 0; // 配送状态
        orderStatus = 0; // 订单状态
        break;
      case 2:
        paymentStatus = 1; // 付款状态
        shippingStatus = 0; // 配送状态
        orderStatus = 1; // 订单状态
        break;
      case 3:
        paymentStatus = 1; // 付款状态
        shippingStatus = 1; // 配送状态
        orderStatus = 1; // 订单状态
        break;
      case 4:
        paymentStatus = 1; // 付款状态
        shippingStatus = 2; // 配送状态
        orderStatus = 2; // 订单状态
        break;
    }
    _this.setData({
      paymentStatus: paymentStatus,
      shippingStatus: shippingStatus,
      orderStatus: orderStatus,
      currentTab: _this.data.currentTab,
    });
    this.loadList();
  },
  loadList() {
    var _this = this;
    this.data.loading = false;
    wx.showLoading({
      title: "加载中",
    });
    list({
      pageNum: _this.data.pageNum,
      pageSize: _this.data.pageSize,
      paymentStatus: _this.data.paymentStatus, // 付款状态
      shippingStatus: _this.data.shippingStatus, // 配送状态
      orderStatus: _this.data.orderStatus, // 订单状态
      mchId: app.globalData.mchId,
    }).then((res) => {
        // console.log(res)
        if (res.type == "success") {
          res.data.data.forEach(function (item) {
            item.itemList.forEach(function (il) {
              il.thumbnail = util.thumbnail(il.thumbnail, 170, 170);
            });
            _this.data.orderList.push(item);
          });
          console.log(_this.data.orderList);
          _this.data.loading = _this.data.pageSize <= res.data.data.length;
          wx.hideLoading();
          _this.setData({
            orderList: _this.data.orderList,
          });
        } else {
          wx.hideLoading();
        }
      })
      .catch((error) => {
        console.log(error);
        this.data.loading = true;
        wx.hideLoading();
      });
  },
  reachBottom() {
    var _this = this;
    if (_this.data.loading == true) {
      this.data.pageNum = this.data.pageNum + 1;
      this.loadList();
    }
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf("iOS") > -1,
      navHeight;
    console.log(sysinfo);
    if (!isiOS) {
      navHeight = 48;
    } else {
      navHeight = 44;
    }
    that.setData({
      status: statusHeight,
      navHeight: navHeight,
    });
  },
  navTo() {
    let p = getCurrentPages().length;
    if (p <= 1) {
      wx.switchTab({
        url: "/pages/index/index",
      });
    } else {
      wx.navigateBack({
        delta: 1,
      });
    }
  },
  refresh() {
    this.bindChange();
  },
});
